package com.iot.controller;

import com.iot.core.JsonHelper;
import com.iot.core.LResult;
import com.iot.mapper.TdeviceMapper;
import com.iot.mapper.Tnb_dev_jsonMapper;
import com.iot.mapper.Twarn_recordMapper;
import com.iot.pojo.*;
import com.iot.util.YT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/test/")
@Controller
public class TestController extends BaseController {


    @Autowired
    Tnb_dev_jsonMapper tnb_dev_jsonMapper;
    @Autowired
    Twarn_recordMapper twarn_recordMapper;
    @Autowired
    TdeviceMapper tdeviceMapper;

    @ResponseBody
    @RequestMapping("send")
    public String send(String str) {
        try {
//            Server server = new Server();
//            server.message = new MqttMessage();
//            server.message.setQos(2);
//            server.message.setRetained(true);
//            server.message.setPayload("可以了abc1".getBytes());
//            server.publish(server.topic , server.message);
//
//            System.out.println(server.message.isRetained() + "------ratained状态");

//            MyMQTT myMQTT=new MyMQTT();
//            myMQTT.subscribe(str+"-test-topic", new OnMessageListener() {
//                @Override
//                public void handleMessage(String topic, String message) {
//                     LResult.Success("完成").toJson();
//                }
//            });
//            myMQTT.publish(str + "-test-topic", "message from me", 0, new OnMessageListener() {
//                @Override
//                public void handleMessage(String topic, String message) {
//
//                }
//            });
            return LResult.Success("完成").toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("send2")
    public String send2(String str) {
        try {

//            MyMQTT myMQTT=new MyMQTT();
//            MNBInfo mnbInfo =new MNBInfo("aaa","00",new Date());
//            myMQTT.publish("NB_DEV_DOWN_DATA", JsonHelper.toJson(mnbInfo), 0, new OnMessageListener() {
//                @Override
//                public void handleMessage(String topic, String message) {
//                    String str=message;
//                }
//            });
            return LResult.Success("完成").toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("change")
    public String change() {
        try {
            String desc = "心跳  ";
            List<MTwarn_record> rs = twarn_recordMapper.fetch(" AND WARN_TYPE_ID=10 AND MESSAGEID='02' AND JSONSTR IS NOT NULL ");
            for (int j = 0; j < rs.size(); j++) {
                MTwarn_record m = rs.get(j);
                MTdevice md = tdeviceMapper.single(m.getDEVICE_ID());
                String imei = md.getIMEI();
                //MTnb_dev_json json = tnb_dev_jsonMapper.singleByWhere(" AND IMEI='" + imei + "' AND MESSAGEID='02' AND MEANCODE=0 AND MESSAGETIME='" + Common.getLongDate(m.getSDATE()) + "'");
                // if (json != null) {
                MTnb_dev_data data = JsonHelper.toBean(m.getJSONSTR(), MTnb_dev_data.class);
                int meanCode = data.getMEANCODE();
                String doors = YT.decimalToBinary(data.getDOORLOCK());
                int mdoors = md.getDOOR_NUM();
                String descDoor = " ";
                //if (m.getDESCS().equals("心跳")) {
                if (meanCode == 2) {
                    for (int i = 0; i < mdoors; i++) {
                        char ch = doors.charAt(i);
                        if (ch == '1') {
                            descDoor += (i + 1) + "号门异常打开 ";
                        }
                    }
                    desc += descDoor;
                } else if (meanCode == 1) {
                    for (int i = 0; i < mdoors; i++) {
                        char ch = doors.charAt(i);
                        if (ch == '1') {
                            descDoor += (i + 1) + "号门被打开 ";
                        }
                    }
                    desc += descDoor;
                } else if (meanCode == 0) {
                    for (int i = 0; i < mdoors; i++) {
                        char ch = doors.charAt(i);
                        if (ch == '1') {
                            descDoor += (i + 1) + "号门被打开 ";
                        } else {
                            descDoor += (i + 1) + "号门关闭 ";
                        }
                    }
                    desc += descDoor;
                }

                String desc2 = (data.getSOAKING() == 1 ? "进水" : "没有进水") + " 电量：" + data.getBATTERYLEVEL() + " 温度 ：" + data.getTEMPERATURE() +
                        " 湿度:" + data.getHUMIDITY() + " 震动值:" + data.getSOAKING() + " 倾斜值:" + data.getTILT() + "电磁锁状态 " + (data.getLOCKSTATUS() ==
                        1 ? "锁开" : "锁关");

                MTwarn_record m2 = new MTwarn_record();
                m2.setWARN_ID(m.getWARN_ID());
                m2.setDESCS(desc + " " + desc2);
                twarn_recordMapper.update(m2);

//                    } else {
//                        MTwarn_record m2 = new MTwarn_record();
//                        m2.setWARN_ID(m.getWARN_ID());
//                        //  m2.setDESCS(m.getDESCS() + " " + desc);
//                        m2.setJSONSTR(json.getJOSNSTR());
//                        twarn_recordMapper.update(m2);
//                    }
                // }
            }

            return "";
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }
}
